package com.jeecg.p3.system.util;

import gui.ava.html.image.generator.HtmlImageGenerator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.*;

public class HtmlUtil {

    /**
     *
     * @Description HTML转Image
     * @param htmText HTML文本字符串
     * @return 希望生成的Image Location
     */
    public static String html2Img(String htmText, String saveImageLocation){
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        try {
            imageGenerator.loadHtml(htmText);
            Thread.sleep(100);
            imageGenerator.getBufferedImage();
            Thread.sleep(200);
            imageGenerator.saveAsImage(saveImageLocation);
            //imageGenerator.saveAsHtmlWithMap("hello-world.html", saveImageLocation);
            //不需要转换位图的，下面三行可以不要
            /*BufferedImage sourceImg = ImageIO.read(new File(saveImageLocation));
            sourceImg = transform_Gray24BitMap(sourceImg);
            ImageIO.write(sourceImg, "BMP", new File(saveImageLocation));*/
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("将HTML文件转换成图片异常");
        }
        return saveImageLocation;
    }

    /**
     *
     * @Description 转换成24位图的BMP
     * @param image
     * @return
     */
    public static BufferedImage transform_Gray24BitMap(BufferedImage image){

        int h = image.getHeight();
        int w = image.getWidth();
        int[] pixels = new int[w * h]; // 定义数组，用来存储图片的像素
        int gray;
        PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
        try {
            pg.grabPixels(); // 读取像素值
        } catch (InterruptedException e) {
            throw new RuntimeException("转换成24位图的BMP时，处理像素值异常");
        }

        for (int j = 0; j < h; j++){ // 扫描列
            for (int i = 0; i < w; i++) { // 扫描行
                // 由红，绿，蓝值得到灰度值
                gray = (int) (((pixels[w * j + i] >> 16) & 0xff) * 0.8);
                gray += (int) (((pixels[w * j + i] >> 8) & 0xff) * 0.1);
                gray += (int) (((pixels[w * j + i]) & 0xff) * 0.1);
                pixels[w * j + i] = (255 << 24) | (gray << 16) | (gray << 8) | gray;
            }
        }

        MemoryImageSource s= new MemoryImageSource(w,h,pixels,0,w);
        Image img =Toolkit.getDefaultToolkit().createImage(s);
        BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);//如果要转换成别的位图，改这个常量即可
        buf.createGraphics().drawImage(img, 0, 0, null);
        return buf;
    }

    public static BufferedImage resizeImage(int x, int y, BufferedImage bufferedImage) {
        BufferedImage bfi = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bfi.getGraphics().drawImage(bufferedImage.getScaledInstance(x,y, Image.SCALE_SMOOTH), 0, 0, null);
        return bfi;
    }

    /**
     *
     * @param backPath 背景图片路径
     * @param backWidth 背景图片宽度
     * @param backHeight 背景图片高度
     * @param qrCodepath 二维码图片路径
     * @param qrCodeWidth 二维码宽度
     * @param qrCodeHeight 二维码高度
     * @param xPositon 二维码位置X坐标
     * @param yPosition 二维码位置Y轴坐标
     * @param outPath 输出位置
     * @return
     */
    public static String convertImage(String backPath, Integer backWidth, Integer backHeight,
                                      String qrCodepath, Integer qrCodeWidth, Integer qrCodeHeight,
                                      Integer xPositon, Integer yPosition, String outPath) {
        try {
            return convertImage(backPath, backWidth, backHeight, qrCodepath, qrCodeWidth, qrCodeHeight,
                    xPositon, yPosition, outPath, false, null, null, null, null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     *
     * @param backPath 背景图片路径
     * @param backWidth 背景图片宽度
     * @param backHeight 背景图片高度
     * @param qrCodepath 二维码图片路径
     * @param qrCodeWidth 二维码宽度
     * @param qrCodeHeight 二维码高度
     * @param xPositon 二维码位置X坐标
     * @param yPosition 二维码位置Y轴坐标
     * @param outPath 输出位置
     * @param haveWord 是否有文字
     * @param fontType 文字类型：宋体，微软雅黑等
     * @param fontSize 字体大小
     * @param color 字体颜色
     * @param context 文字内容
     * @param wordX 文字相对X位置
     * @param wordY 文字相对Y位置
     * @return
     */
    public static String convertImage(String backPath, Integer backWidth, Integer backHeight,
                                      String qrCodepath, Integer qrCodeWidth, Integer qrCodeHeight,
                                      Integer xPositon, Integer yPosition, String outPath,
                                      Boolean haveWord, String fontType, Integer fontSize,
                                      Color color, String context, Integer wordX, Integer wordY) {
        try {
            BufferedImage background = resizeImage(backWidth,backHeight, ImageIO.read(new File(backPath)));
            BufferedImage qrcode = resizeImage(qrCodeWidth,qrCodeHeight, ImageIO.read(new File(qrCodepath)));
            Graphics2D g = background.createGraphics();
            g.drawImage(qrcode, xPositon, yPosition, qrcode.getWidth(), qrcode.getHeight(),null);
            if(haveWord) {
                Font f = new Font(fontType, Font.PLAIN, fontSize);
                FontMetrics metrics = g.getFontMetrics(f);
                int x = (backWidth - metrics.stringWidth(context)) / 2;
                Color mycolor = color;
                g.setColor(mycolor);
                g.setFont(f);
                g.drawString(context, x, wordY);
            }
            g.dispose();
            ImageIO.write(background, "jpg", new File(outPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     *
     * @param backPath 背景图片路径
     * @param qrCodepath 二维码路径
     * @param outPath
     * @return
     */
    public static String convertImg(String backPath, String qrCodepath, String outPath) {
        try {
            /*黑底海报
            BufferedImage background = resizeImage(509,905, ImageIO.read(new File(backPath)));
            BufferedImage qrcode = resizeImage(210,210, ImageIO.read(new File(qrCodepath)));
            Graphics2D g = background.createGraphics();
            g.drawImage(qrcode, 148, 450, qrcode.getWidth(), qrcode.getHeight(),null);*/
            //蓝底海报
            BufferedImage background = resizeImage(509,905, ImageIO.read(new File(backPath)));
            BufferedImage qrcode = resizeImage(130,130, ImageIO.read(new File(qrCodepath)));
            Graphics2D g = background.createGraphics();
            g.drawImage(qrcode, 59, 626, qrcode.getWidth(), qrcode.getHeight(),null);
            g.dispose();
            ImageIO.write(background, "jpg", new File(outPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convertImg1(String backPath, String qrCodepath, String outPath) {
        try {
            BufferedImage background = resizeImage(509,905, ImageIO.read(new File(backPath)));
            BufferedImage qrcode = resizeImage(200,200, ImageIO.read(new File(qrCodepath)));
            Graphics2D g = background.createGraphics();
            g.drawImage(qrcode, 140, 400, qrcode.getWidth(), qrcode.getHeight(),null);
            g.dispose();
            ImageIO.write(background, "jpg", new File(outPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String test(String backPath, String qrCodepath, String outPath) {
        try {
            BufferedImage background = resizeImage(509,905, ImageIO.read(new File(backPath)));
            BufferedImage qrcode = resizeImage(200,200, ImageIO.read(new File(qrCodepath)));
            Graphics2D g = background.createGraphics();
            g.drawImage(qrcode, 155, 390, qrcode.getWidth(), qrcode.getHeight(),null);
            Font f = new Font("黑体", Font.PLAIN, 30);
            FontMetrics metrics = g.getFontMetrics(f);
            int x = (509 - metrics.stringWidth("中医基础学公开课第一节")) / 2;
            Color mycolor = new Color(205,48,48);
            g.setColor(mycolor);
            g.setFont(f);
            g.drawString("中医基础学公开课第一节", x, 150);
            g.dispose();
            ImageIO.write(background, "jpg", new File(outPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        /*String saveImageLocation = "E:\\img\\photo.png";
        String htmText = "<html><body><div class='logImg' style='margin:0px;left:0px;top:2px;text-align: center;width: 350px;'><img align='middle' src='file:///E:/img/test.jpg'></div></body></html>";
        html2Img(htmText, saveImageLocation);*/
        test("E:\\img\\background.png", "E:\\img\\ceshi.jpg", "E:\\img\\yd.jpg");


    }
}
